﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace Bierdopje.API.Personal
{
    public class CalendarEpisode
    {
        private string m_ShowName;

        public string ShowName
        {
            get { return m_ShowName; }
            set { m_ShowName = value; }
        }
        private string m_ShowUrl;

        public string ShowUrl
        {
            get { return m_ShowUrl; }
            set { m_ShowUrl = value; }
        }
        private string m_EpisodeSEString;

        public string EpisodeSEString
        {
            get { return m_EpisodeSEString; }
            set { m_EpisodeSEString = value; }
        }
        private string m_EpisodeName;

        public string EpisodeName
        {
            get { return m_EpisodeName; }
            set { m_EpisodeName = value; }
        }
        private string m_EpisodeUrl;

        public string EpisodeUrl
        {
            get { return m_EpisodeUrl; }
            set { m_EpisodeUrl = value; }
        }

        private ApiLibrary m_APILibrary;

        [XmlIgnore]
        public ApiLibrary APILibrary
        {
            get { return m_APILibrary; }
            set { m_APILibrary = value; }
        }

        public CalendarEpisode(ApiLibrary api)
        {
            m_APILibrary = api;
        }

        public CalendarEpisode()
        {
            
        }

        private ApiShow m_ApiShow;

        [XmlIgnore]
        public ApiShow ApiShow
        {
            get 
            {
                if (m_APILibrary == null)
                    throw new EApiLibraryNotPresent();

                if (m_ApiShow == null)
                {
                    m_ApiShow = m_APILibrary.GetShowById(this.ShowID);
                    //List<ApiShow> _Shows = m_API.FindShowByName(this.ShowName);
                    //DateTime maxAirDate = DateTime.MinValue;
                    //foreach (ApiShow _Show in _Shows)
                    //{
                    //    if (_Show.NextEpisode > maxAirDate)
                    //    {
                    //        maxAirDate = _Show.NextEpisode;
                    //        m_ApiShow = _Show;
                    //    }
                    //}
                }
                return m_ApiShow;
            }
        }

        private ApiEpisode m_ApiEpisode;

        [XmlIgnore]
        public ApiEpisode ApiEpisode
        {
            get
            {
                if (m_ApiEpisode == null)
                {
                    SubHelper.EpisodeString _EpString = SubHelper.GetEpInfoFromSEString(this.m_EpisodeSEString);

                    if (ApiShow == null) return null;

                    m_ApiEpisode = ApiShow.GetEpisode(_EpString.Season, _EpString.Episode);
                }
                return m_ApiEpisode;
            }
        }

        private int m_ShowID;

        public int ShowID
        {
            get { return m_ShowID; }
            set { m_ShowID = value; }
        }
        private int m_EpisodeID;

        public int EpisodeID
        {
            get { return m_EpisodeID; }
            set { m_EpisodeID = value; }
        }
    }
}
